import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts, TitleOpts, LabelOpts
# 读取文件
f = open("D:\\file\\疫情.txt", "r", encoding="utf-8")
data = f.read()
# 关闭文件
f.close()
# 获取河南省数据
# 将字符串转换为字典
data_dict = json.loads(data)
hn_data_list = data_dict["areaTree"][0]["children"][3]["children"]
# 准备数据为元组放入list列表中
data_list = list()
for hn_data in hn_data_list:
    city_name = hn_data["name"]
    if city_name != "境外输入" and city_name != "地区待确认":
        city_name += "市"
    city_confirm = hn_data["total"]["confirm"]
    data_list.append((city_name, city_confirm))
data_list.append(("济源市", 5))
print(data_list)
# 构建地图
map = Map()
map.add("河南省疫情确诊人数", data_list, "河南", label_opts=LabelOpts(is_show=False))
# 设置全局选项
map.set_global_opts(
    title_opts=TitleOpts(title="河南省疫情分布图", pos_left="center", pos_bottom="1%"),
    # 视觉映射配置
    visualmap_opts=VisualMapOpts(
        is_show=True,       # 是否显示
        is_piecewise=True,  # 是否分段
        pieces=[
            {"min": 1, "max": 99, "label": "1~99人", "color": "#CCFFFF"},
            {"min": 100, "max": 999, "label": "100~999人", "color": "#FFFF99"},
            {"min": 1000, "max": 4999, "label": "1000~4999人", "color": "#FF9966"},
            {"min": 5000, "max": 9999, "label": "5000~9999人", "color": "#FF6666"},
            {"min": 10000, "max": 99999, "label": "10000~99999人", "color": "#CC3333"},
            {"min": 100000, "label": "100000~人", "color": "#990033"},
        ]
    ),

)

# 绘图
map.render("河南省疫情分布图.html")

